package site.yunnong.atvris.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import site.yunnong.atvris.dao.admin.service.SysUserManageService;
import site.yunnong.atvris.dao.admin.vo.UserVo;
import site.yunnong.atvris.dao.common.exception.YunnongException;
import site.yunnong.atvris.dao.common.param.UserQueryParam;
import site.yunnong.atvris.dao.common.util.LocalAssert;
import site.yunnong.atvris.dao.model.*;
import site.yunnong.atvris.dao.mapper.UmsUserInterestTagMapper;
import site.yunnong.atvris.dao.mapper.UmsUserMapper;
import site.yunnong.atvris.model.*;
import site.yunnong.atvris.service.MediaPartitionService;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author JingHaoZhang
 * @date 2021-06-21
 */
@Service
public class SysUserManageServiceImpl implements SysUserManageService {

    @Resource
    private UmsUserMapper userMapper;
    @Resource
    private UmsUserInterestTagMapper userInterestTagMapper;
    @Resource
    private UmsUserInterestTagMapper umsUserInterestTagMapper;
    @Resource
    private MediaPartitionService mediaPartitionService;

    @Override
    public List<UserVo> query(UserQueryParam param, Integer pageNum, Integer pageSize) {
        UmsUserExample example = new UmsUserExample();
        if (param.getRole() != null) {
            example.createCriteria().andRoleEqualTo(param.getRole());
        }
        if (param.getNickname() != null) {
            example.createCriteria().andNicknameEqualTo(param.getNickname());
        }
        if (param.getStatus() != null) {
            example.createCriteria().andStatusEqualTo(param.getStatus());
        }
        if (param.getGender() != null) {
            example.createCriteria().andCityEqualTo(param.getCity());
        }
        PageHelper.startPage(pageNum, pageSize);
        List<UmsUser> userList = userMapper.selectByExample(example);


        userList.stream().map(umsUser -> {
            UserVo vo = new UserVo();
            BeanUtils.copyProperties(umsUser, vo);
            Long uid = umsUser.getId();
            // 感兴趣的分区
            List<String> interestPartitions = getUserInterestPartitionNames(uid);
            vo.setInterestTags(interestPartitions);
            UmsUserProfile profile = getUserProfile(uid);
            fillProfile(vo, profile);
        }, );
        return
    }

    private UmsUserProfile getUserProfile(Long uid) {
        UmsUserProfileExample example = new UmsUserProfileExample();
        example.createCriteria().andUidEqualTo(uid);
        umsUser

    }

    @Override
    public int changePermission(Long id, Integer status) {
        LocalAssert.notNull(status, "输入参数错误");
        UmsUser user = userMapper.selectByPrimaryKey(id);
        LocalAssert.notNull(user, "无此用户");
        if (user.getStatus().equals(status)) {
            throw new YunnongException("用户已处于目标状态");
        }
        user.setStatus(status);
        return userMapper.updateByPrimaryKey(user);
    }

    public List<String> getUserInterestPartitionNames(Long uid) {
        UmsUserInterestTagExample example = new UmsUserInterestTagExample();
        example.createCriteria().andUidEqualTo(uid);
        List<UmsUserInterestTag> interestTagList = userInterestTagMapper.selectByExample(example);
        List<String> partitionNames = Lists.newArrayList();
        for (UmsUserInterestTag interestTag : interestTagList) {
            Long pid = interestTag.getPid();
            // 分区名
            String partitionName = mediaPartitionService.queryPartitionNameById(pid);
            partitionNames.add(partitionName);
        }
        return partitionNames;
    }
}
